package com.voicesysapp.tripledes;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.net.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TripleDesUtils{

    private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish
    private static final String securekey = "zyzx@yygk"; //

    public static void main(String[]args){
        System.out.println(encode3Des("222"));
    }
    /**
     * 转换成十六进制字符串
     *
     * @param key
     * @return
     * lee on 2017-08-09 10:54:19
     */
    private static byte[] hex(String key) {
        String f = DigestUtils.md5Hex(key);
        byte[] bkeys = new String(f).getBytes();
        byte[] enk = new byte[24];
        for (int i = 0; i < 24; i++) {
            enk[i] = bkeys[i];
        }
        return enk;
    }

    /**
     * 3DES加密
     *
     * @param srcStr
     *            将加密的字符串
     * @return
     *
     *         lee on 2017-08-09 10:51:44
     */
    public static String encode3Des(String srcStr) {
        byte[] keybyte = hex(securekey);
        byte[] src = srcStr.getBytes();
        try {
            // 生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            // 加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);

            String pwd = Base64.encodeBase64String(c1.doFinal(src));
            // return c1.doFinal(src);//在单一方面的加密或解密
            return pwd;
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    /**
     * 3DES解密
     *
     * @param desStr
     *            解密后的字符串
     * @return
     *
     *         lee on 2017-08-09 10:52:54
     */
    public static String decode3Des(String desStr) {
        Base64 base64 = new Base64();
        byte[] keybyte = hex(securekey);
        byte[] src = base64.decode(desStr);
        try {
            // 生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            // 解密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            String pwd = new String(c1.doFinal(src));
            // return c1.doFinal(src);
            return pwd;
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
}
